package com.hyhy.hycloud.uaa.security.provider;

import com.hyhy.hycloud.commons.util.text.TextValidator;
import com.hyhy.hycloud.uaa.security.PasswordType;
import com.hyhy.hycloud.uaa.security.exception.BadAuthVerificationCodeException;
import com.hyhy.hycloud.uaa.security.exception.UserNotFoundException;
import com.hyhy.hycloud.uaa.service.AppIdConfigFinder;
import com.hyhy.hycloud.uaa.service.SmsVerificationCodeByMobileFinder;
import com.hyhy.hycloud.uaa.service.UserDetailsByMobileFinder;
import com.hyhy.hycloud.uaa.service.UserRegister;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.List;

import static com.hyhy.hycloud.uaa.Constants.LOGIN_BIZ_CODE;

/**
 * 短信验证码认证提供者
 *
 * @author Xuegui Yuan
 * @date 2018-09-19
 */
@Builder
public class SmsVerificationCodeAuthenticationProvider extends AbstractAuthenticationProvider {

    private SmsVerificationCodeByMobileFinder smsVerificationCodeByMobileFinder;

    private UserDetailsByMobileFinder userDetailsByMobileFinder;

    private UserRegister userRegister;
    
    private AppIdConfigFinder appIdConfigFinder;

    @Override
    boolean isSupportAuthRequest(PasswordType passwordType, String appId, String principal) {
        List<String> supportedAppIdList = appIdConfigFinder.supportSmsVerificationCodeList();
        return (PasswordType.VERIFICATION_CODE == passwordType || PasswordType.UNDEFINED == passwordType)
                && (supportedAppIdList != null && supportedAppIdList.contains(appId))
                && TextValidator.isMobileSimple(principal);
    }

    @Override
    UserDetails authenticationCredentials(String appId,
                                          String principal, String credentials) {
        UserDetails userDetails;
        List<String> autoRegisterApps = appIdConfigFinder.autoRegisterList();
        if (!autoRegisterApps.isEmpty() && StringUtils.isNoneBlank(appId) && autoRegisterApps.contains(appId)) {
            String code = smsVerificationCodeByMobileFinder.findSmsVerificationCode(LOGIN_BIZ_CODE, principal);
            if (!credentials.equals(code)) {
                throw new BadAuthVerificationCodeException("Invalid SMS verification Code");
            }
            try {
                userDetails = userDetailsByMobileFinder.findUserDetailsByMobile(appId, principal);
            } catch (UserNotFoundException unf) {
                // 注册新用户
                userDetails = userRegister.registerUser(appId, principal);
            }
        } else {
            userDetails = userDetailsByMobileFinder.findUserDetailsByMobile(appId, principal);
            String code = smsVerificationCodeByMobileFinder.findSmsVerificationCode(LOGIN_BIZ_CODE, principal);
            if (!credentials.equals(code)) {
                throw new BadAuthVerificationCodeException("Invalid SMS verification Code");
            }
        }
        return userDetails;
    }
}
